﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SCMS.Model;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;

namespace SCMS.UI.Models
{
    public class TravelAuth
    {
        public TravelAuthorization EntityTravelAuth { get; set; }

        public SelectList staffs { get; set; }

        public SelectList Currencies { get; set; }

        public List<Model.TravelDate> TravelDates { get; set; }

        public List<Model.TravelExpense> TravelExpense { get; set; }
    }

    public class TravelDate
    {
        public Model.TravelDate EntityTDate { get; set; }

        public SelectList Currencies { get; set; }

        public Guid ProjectId { get; set; }

        public SelectList ProjectNos { get; set; }

        public SelectList BudgetLines { get; set; }

        public Guid TravelAuthorizationId { get; set; }
    }

    public class TravelExpense
    {
        public Model.TravelExpense EntityTExpense { get; set; }

        public SelectList Currencies { get; set; }

        public Guid ProjectId { get; set; }

        public SelectList ProjectNos { get; set; }

        public SelectList BudgetLines { get; set; }

        public Guid TravelAuthorizationId { get; set; }
    }

    public class ViewTAForms
    {
        public Model.TravelAuthorization TAuthFormEntity { get; set; }

        public List<Model.TravelDate> TravelDates { get; set; }

        public List<Model.TravelExpense> TravelExpenses { get; set; }

        public List<Models.TravelDate> ViewTravelDates { get; set; }

        public List<Models.TravelExpense> ViewTravelExpenses { get; set; }

        public VStaffDetail  Travelller { get; set; }

        public VStaffDetail PreparedBy { get; set; }

        public VStaffDetail ApprovedBy { get; set; }

        public VStaffDetail AuthorizedBy { get; set; }

        public bool CanApprove { get; set; }

        public bool CanPostFunds { get; set; }

        [Required]
        public Guid MBCurrencyId { get; set; }

        public string MBCurrency { get; set; }

        [Required]
        public decimal MBValue { get; set; }

        public SelectList Currencies { get; set; }
    }

    public class TAFExtention
    {
        public static ViewTAForms PrepareTAF(Guid TAFid)
        {
            using (var db = new SCMSEntities())
            {
                Model.TravelAuthorization TAF = db.TravelAuthorizations.FirstOrDefault(p => p.Id == TAFid);
                Model.Currency tafc = TAF.Currency;
                List<Model.TravelExpense> TAFExpenses = TAF.TravelExpenses.ToList();
                foreach (var item in TAFExpenses)
                {
                    Model.Currency c = item.Currency;
                    Model.ProjectBudget pb = item.ProjectBudget;
                    Model.BudgetCategory bc = pb.BudgetCategory;
                    Model.ProjectDonor pd = bc.ProjectDonor;
                }
                List<Model.TravelDate> TAFDates = TAF.TravelDates.ToList();
                foreach (var item in TAFDates)
                {
                    Model.Currency c = item.Currency;
                    Model.ProjectBudget pb = item.ProjectBudget;
                    Model.BudgetCategory bc = pb.BudgetCategory;
                    Model.ProjectDonor pd = bc.ProjectDonor;
                }
                var modelItem = new ViewTAForms()
                {
                    TAuthFormEntity = TAF,
                    Travelller = db.VStaffDetails.FirstOrDefault(p => p.StaffID == TAF.TravellerId),
                    PreparedBy = db.VStaffDetails.FirstOrDefault(p => p.StaffID == TAF.PreparedBy),
                    ApprovedBy = db.VStaffDetails.FirstOrDefault(p => p.StaffID == TAF.ApprovedBy),
                    AuthorizedBy = db.VStaffDetails.FirstOrDefault(p => p.StaffID == TAF.AuthorizedBy),
                    TravelDates = TAFDates,
                    TravelExpenses = TAFExpenses,
                    MBCurrency = TAF.Currency1 != null ? TAF.Currency1.ShortName : ""
                };
                return modelItem;
            }
        }
    }
}